package com.yanqu.road.server.manger.activity.monjinlottery;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.selectlottery.config.SelectLotteryConfig;
import com.yanqu.road.entity.activity.selectlottery.config.SelectLotteryGoodsConfig;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.DaoHelper;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SelectLotteryProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.apprentice.ApprenticeModule;
import com.yanqu.road.server.gameplayer.module.activity.selectlottery.SelectLotteryModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import org.checkerframework.checker.units.qual.A;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SelectLotteryMgr extends TempMgr {

    /**
     * 关联活动的config配置key
     */
    private static final String relationActivityKey = "BIND_LOTTERY_TYPE";

    /**
     * 配置MAP
     */
    private static Map<Integer, SelectLotteryConfig> configMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    /**
     * 加载活动配置
     */
    public static void reloadActivityData() {
        getLogger().info("reload selectLottery Activity start");
        //获取活动
        List<ActivityInfo> activityList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.SelectLottery.getValue());
        Map<Integer, SelectLotteryConfig> newActivityConfigMap = new ConcurrentHashMap<>();
        for (ActivityInfo activityInfo : activityList) {
            //获取config
            Map<String, ActivityConfig> configMap = NormalActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
            //奖励配置
            Map<Integer, SelectLotteryGoodsConfig> selectLotteryGoodsConfigMap = DaoHelper.getGenericConfigDaoImpl().getGenericConfigMap(activityInfo.getActivityId(), SelectLotteryGoodsConfig.class);
            //实例化活动配置
            SelectLotteryConfig selectLotteryConfig = new SelectLotteryConfig(activityInfo, configMap, selectLotteryGoodsConfigMap);
            newActivityConfigMap.put(activityInfo.getActivityId(), selectLotteryConfig);
        }
        configMap = newActivityConfigMap;
        //同步一下在线玩家
        List<GamePlayer> playerList = GamePlayerMgr.getAllOnlinePlayer();
        for (GamePlayer gamePlayer : playerList) {
            gamePlayer.getModule(SelectLotteryModule.class).syncConfig();
        }
        getLogger().info("reload selectLottery Activity end");
    }

    /**
     * 获取配置MAP
     * @return
     */
    public static Map<Integer, SelectLotteryConfig> getConfigMap() {
        return configMap;
    }

    /**
     * 获取配置
     * @param activityId
     * @return
     */
    public static SelectLotteryConfig getConfig(int activityId) {
        return configMap.get(activityId);
    }

    /**
     * 【自选抽奖活动】关联的主活动LIST
     * @param activityId 抽奖活动ID
     * @return
     */
    public static List<ActivityInfo> getRelationActivity(int activityId) {
        List<ActivityInfo> relationActivityList = new ArrayList<>();
        SelectLotteryConfig selectLotteryConfig = getConfig(activityId);
        if (selectLotteryConfig == null) {
            return relationActivityList;
        }
        if (!ActivityHelper.activityInShowTime(selectLotteryConfig.getActivityInfo())) {
            return relationActivityList;
        }
        //获取当前开启的活动
        Map<Integer, ActivityInfo> activityInfoList = NormalActivityMgr.getShowTimeActivityInfoMap();
        for (ActivityInfo activityInfo : activityInfoList.values()) {
            if (!ActivityHelper.activityInShowTime(activityInfo)) {
                continue;
            }
            Map<String, ActivityConfig> configMap = NormalActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
            if (configMap == null) {
                continue;
            }
            //判断活动是否有关联【自选抽奖活动】
            ActivityConfig activityConfig = configMap.get(relationActivityKey);
            if (activityConfig == null) {
                continue;
            }
            //模版ID,也就是【自选抽奖活动】的活动小类型id
            int tempId = activityConfig.getIntValue();
            //判断模版ID是否一致
            if (tempId == selectLotteryConfig.getActivityInfo().getChildType()) {
                relationActivityList.add(activityInfo);
            }
        }
        return relationActivityList;
    }

    /**
     * 同步公告
     * @param noticeData
     */
    public static void syncNotice(SelectLotteryProto.SelectLotteryNoticeData noticeData) {
        SelectLotteryProto.SelectLotterySyncNoticeMsg.Builder noticeDataBuilder = SelectLotteryProto.SelectLotterySyncNoticeMsg.newBuilder();
        noticeDataBuilder.setActivityId(noticeData.getActivityId());
        noticeDataBuilder.setNotice(noticeData.toBuilder());
        //同步在线玩家
        List<GamePlayer> onlinePlayerList = GamePlayerMgr.getAllOnlinePlayer();
        for (GamePlayer gamePlayer : onlinePlayerList) {
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_SELECT_LOTTERY_SYNC_NOTICE, noticeDataBuilder);
            GamePlayerMgr.sendPacket(gamePlayer.getUserId(), pbMsg);
        }
    }
}
